package com.dfbz.bigdata.modules.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfbz.bigdata.common.utils.R;
import com.dfbz.bigdata.modules.system.doman.TbComment;
import com.dfbz.bigdata.modules.system.doman.UserBrand;
import com.dfbz.bigdata.modules.system.doman.UserDesignerandpersonal;
import com.dfbz.bigdata.modules.system.service.TbCommentService;

import com.dfbz.bigdata.modules.system.service.UserBrandService;
import com.dfbz.bigdata.modules.system.service.UserDesignerandpersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * (TbComment)表控制层
 *
 * @author makejava
 * @since 2023-03-18 21:34:24
 */
@RestController
@RequestMapping("/goods/tbComment")
public class TbCommentController {
    /**
     * 服务对象
     */
    @Resource
    private TbCommentService tbCommentService;

    @Autowired
    private UserBrandService userBrandService;

    @Autowired
    private UserDesignerandpersonalService userDesignerandpersonalService;

    /**
     * 分页查询
     *
     * @param tbComment 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<PageImpl<TbComment>> queryByPage(TbComment tbComment, PageRequest pageRequest) {
        return null;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TbComment> queryById(@PathVariable("id") String id) {
        return null;
    }

    /**
     * 新增数据
     *
     * @param tbComment 实体
     * @return 新增结果
     */
    @PostMapping
    public R<String> add(@RequestBody TbComment tbComment) {
        tbComment.setCommentId(UUID.randomUUID().toString());
        tbComment.setCommentCreattime(new Date());
        tbCommentService.save(tbComment);


        return R.success("新增成功");
    }


    @GetMapping("/three/{goodid}")
    public R<Map> getcomment(@PathVariable("goodid") String goodid){
        LambdaQueryWrapper<TbComment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TbComment::getCommentGoodcaseId,goodid);
        List<TbComment> list = tbCommentService.list(lqw);
        List<TbComment> newList=new ArrayList<>();
        if (list.size()<3){
            newList=list;
        }else {
            newList = list.subList(1, 3);
        }

        for (int i=0;i<newList.size();i++){
            TbComment tbComment = newList.get(i);
            String commentUserid = tbComment.getCommentUserid();
            UserDesignerandpersonal byId = userDesignerandpersonalService.getById(commentUserid);
            String userIcon = byId.getUserIcon();
            tbComment.setCommentImage(userIcon);
            tbComment.setCommentId(byId.getUserName());
        }
        Map map=new HashMap();
        map.put("data",newList);
        map.put("count",list.size());
        return R.success(map);
    }

    @GetMapping("{all/goodid}")
    public R<List> getallcomment(@PathVariable("goodid") String goodid){
        LambdaQueryWrapper<TbComment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TbComment::getCommentGoodcaseId,goodid);
        List<TbComment> list = tbCommentService.list(lqw);
        return R.success(list);
    }
    /**
     * 编辑数据
     *
     * @param tbComment 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<TbComment> edit(TbComment tbComment) {
        return null;
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String id) {
        return null;
    }

}

